package com.waq.service.impl;

import com.waq.enums.ConsultationType;
import com.waq.mapper.CustomerMapper;
import com.waq.mapper.RegisterMapper;
import com.waq.pojo.Customer;
import com.waq.pojo.Register;
import com.waq.pojo.User;
import com.waq.pojo.bo.RegisterBO;
import com.waq.service.RegisterService;
import com.waq.utils.ConvertUtils;
import com.waq.utils.PinyinUtil;
import org.apache.commons.lang3.StringUtils;
import org.n3r.idworker.Sid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import static com.waq.utils.ConvertUtils.convertRegisterToCustomer;

@Service
@Transactional
public class RegisterServiceImpl implements RegisterService {
    @Autowired
    private RegisterMapper registerMapper;
    @Autowired
    private CustomerMapper customerMapper;
    @Autowired
    private Sid sid;

    public List<Register> queryRegisterList(Register register) {
        if (register.getConsultationType() == ConsultationType.ALL.type) {
            register.setConsultationType(null);
        }
        List<Register> result = registerMapper.queryRegisterList(register);
        return result;
    }

    /**
     * 新增登记，同时更新客户公共信息表
     * @param register
     * @return
     */
    public Register registerCustomer(Register register) {
        String registerId = sid.nextShort();//设置主键id（固定16位字母数字混编的字符串）
        register.setRegisterId(registerId);
        //之前是否登记过
        if (register.getCustomerId() != null) {
            //TODO 登记过
            //1.查找到之前登记过的会员号
            register.setCustomerId(register.getCustomerId());
            //2.将登记信息放入登记表
            int result = registerMapper.insertSelective(register);
            if (result != 1) {
                return null;
            }
            //3.如果更新了客户公共信息，该客户的所有登记信息也要一并更新
            registerMapper.updateCommonRegister(register);
            //4.同时客户信息表也要更新
            Customer customer = new Customer();
            convertRegisterToCustomer(register, customer);
            customer.setHasConsulted(0);
            customerMapper.changeCustomerByRegister(customer);
        } else {
            //TODO 未登记过
            int customerId = 0;
            //1.新增会员号
            do {
                customerId = sid.nextInt();
            } while (customerMapper.existsWithPrimaryKey(customerId));
            register.setCustomerId(customerId);
            //2.将登记信息放入登记表
            int result = registerMapper.insertSelective(register);
            if (result != 1) {
                return null;
            }
            //3.同时客户信息表需要新增客户信息
            Customer customer = new Customer();
            customer.setFirstVisitTime(register.getTreatmentTime());
            convertRegisterToCustomer(register, customer);
            customer.setHasConsulted(0);//前台登记过，未咨询
            customerMapper.insertSelective(customer);
        }
        return register;
    }

    /**
     * 修改登记信息，同时更新客户公共信息
     * @param register
     * @return
     */
    @Override
    public Register changeRegister(Register register) {
        int result = registerMapper.changeRegister(register);
        if (result <= 0) {
            return null;
        }
        //更新该客户登记表中所有公共信息
        registerMapper.updateCommonRegister(register);
        //同时客户信息表也要更新
        customerMapper.changeCustomerByRegister(convertRegisterToCustomer(register, new Customer()));
        return register;
    }

//    private void convertBOToRegister(Register register, RegisterBO registerBO) {
//        register.setCustomerName(registerBO.getCustomerName());
//        register.setCustomerPinyin(PinyinUtil.getAllPinyin(register.getCustomerName()));
//        register.setGender(registerBO.getGender());
//        register.setAge(registerBO.getAge());
//        register.setTelephone(registerBO.getTelephone());
////        register.setTransactionStatus(registerBO.getTransactionStatus());
//        //register.setMembership(registerBO.getMembership()); TODO 会员是计算总消费额判断等级
//        register.setConsultant(registerBO.getConsultant());
//        register.setMarketer(registerBO.getMarketer());
//        register.setBeautyParlour(registerBO.getBeautyParlour());
//        register.setReferee(registerBO.getReferee());
//        register.setRefereeTel(registerBO.getRefereeTel());
//        register.setDoctor(registerBO.getDoctor());
//        register.setConsultationType(registerBO.getConsultationType());
//        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd 'at' HH:mm:ss z");
//        register.setTreatmentTime(new Date(System.currentTimeMillis()));
//    }




}
